<template>
  <div>
    <!-- 评论列表 -->
    <div
      class="cmt-list"
      :class="commentShow ? 'art-cmt-container-2' : 'art-cmt-container-1'"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
        :immediate-check="false"
      >
        <!-- 评论的 Item 项 -->
        <div class="cmt-item" v-for="item in commentList" :key="item.com_id">
          <!-- 头部区域 -->
          <div class="cmt-header">
            <!-- 头部左侧 -->
            <div class="cmt-header-left">
              <img :src="item.aut_photo" alt="" class="avatar" />
              <span class="uname">{{ item.aut_name }}</span>
            </div>
            <!-- 头部右侧 -->
            <div class="cmt-header-right">
              <van-icon
                name="like"
                size="16"
                color="red"
                v-if="item.is_liking"
                @click="loveFn(1, item)"
              />
              <van-icon
                name="like-o"
                size="16"
                color="gray"
                v-else
                @click="loveFn(2, item)"
              />
            </div>
          </div>
          <!-- 主体区域 -->
          <div class="cmt-body">
            {{ item.content }}
          </div>
          <!-- 尾部区域 -->
          <div class="tagBox">
            <van-tag plain type="primary" @click="cpopShow(item)">
              回复<van-icon name="arrow" />
            </van-tag>
            <div class="cmt-footer">{{ item.pubdate }}</div>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 弹出框 -->
    <van-popup v-model="popShow" position="right" get-container="body">
      <Reply @changeShow="changeShowFn"></Reply>
    </van-popup>

    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="commentShow">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="commentShow = false">发表评论</div>
      <div class="icon-box">
        <van-badge :content="total_count" :max="99">
          <van-icon name="comment-o" size="0.53333334rem" @click="moveFn" />
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>
    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-model="commentText"
        v-inp
        @blur="bulrFn"
      ></textarea>
      <van-button
        type="default"
        :disabled="commentText.length === 0"
        @click="releaseFn"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import { getCommentsAPI, releaseAPI, likeAPI, dislikeAPI } from '@/api'
import { mapMutations } from 'vuex'
import { timeAgo } from '@/utils/date'
import Reply from './Reply.vue'
export default {
  components: {
    Reply
  },
  data () {
    return {
      popShow: false,
      commentText: '',
      commentShow: true,
      commentList: [],
      total_count: 0, // 评论数量
      loading: false, // 底部加载状态
      finished: false, // 底部是否加载完成
      offset: null,
      style: ''
    }
  },
  created () {
    this.getCommentsFn()
  },
  methods: {
    ...mapMutations('reply', ['setreplyCommentMutations']),
    // 加载更多
    onLoad () {
      this.getCommentsFn()
    },
    timeAgo,
    // 获取评论列表
    async getCommentsFn () {
      const obj = {
        type: 'a',
        source: this.$route.query.artId,
        offset: this.offset
      }
      const res = await getCommentsAPI(obj)
      this.total_count = res.data.data.total_count || ''
      if (res.data.data.last_id === res.data.data.end_id) {
        console.log(1)
        this.finished = true
      }
      res.data.data.results.forEach(item => {
        this.commentList.push(item)
      })
      this.offset = res.data.data.last_id
      this.loading = false
    },
    // 点击移动到评论区位置
    moveFn () {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    },
    // 失焦事件
    bulrFn () {
      // 问题：先失去焦点了, 而且你用v-if, DOM移除, 点击事件未来的及触发
      // 解决方案：失去焦点, 延时执行
      setTimeout(() => {
        this.commentShow = true
      }, 0)
    },
    // 发布
    async releaseFn () {
      const res = await releaseAPI({
        target: this.$route.query.artId,
        content: this.commentText
      })
      this.commentList.unshift(res.data.data.new_obj)
      this.total_count++
      console.log(res.data.data.new_obj)
      this.commentText = ''
    },
    // 喜欢
    async loveFn (key, obj) {
      // is_liking false 表示不喜欢
      if (key === 1) {
        obj.is_liking = false
        await dislikeAPI(obj.com_id)
      } else {
        obj.is_liking = true
        await likeAPI(obj.com_id)
      }
    },
    changeShowFn (bool) {
      this.popShow = bool
    },
    cpopShow (item) {
      // this.popShow = true
      this.$router.push({
        path: '/reply'
      })
      this.setreplyCommentMutations(item)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
}
.tagBox {
  display: flex;
  align-items: center;
  padding-left: 24px;
  .van-tag {
    display: flex;
    justify-content: center;
    margin-right: 5px;
    width: 40px;
    border-radius: 10px;
    color: #333;
    background: #f7f8fa;
    height: 20px;
  }
  .van-tag--plain::before {
    border: none;
  }
}
.cmt-list {
  padding: 10px;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
    }
  }
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
